﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccess;
using System.Data;
using Magazijn_Layer;

namespace Reparatie
{
    public class ReparatieClass : I_Reparatie
    {
        Magazijn magazijn = new Magazijn();
        Database database = new Database();

        public ReparatieClass()
        {
                
        }

        /// <summary>
        /// Repareer-methode
        /// </summary>
        /// <param name="id">Order id</param>
        /// <param name="aantal">Aantal producten</param>
        public void Repareer(int id)
        {
            DataTable product = database.Select("SELECT Id FROM Product WHERE Id IN (SELECT ProductId FROM ProductOrder WHERE OrderId = " + id + ")");
            DataTable aantal = database.Select("SELECT aantal FROM ProductOrder WHERE OrderId = " + id);

            foreach (DataRow pId in product.Rows)
            {
                DataTable result = magazijn.Zoek(Convert.ToInt32(pId["Id"]));
                if (Convert.ToInt32(result.Rows[0][4]) >= Convert.ToInt32(aantal.Rows[0][0]))
                {
                    magazijn.Verwijderen(Convert.ToInt32(pId[0]), Convert.ToInt32(aantal.Rows[0][0]));
                }
            }
        }


    }
}
